package com.kanjj.mywebrtc.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kanjj.mywebrtc.ChatActivity;
import com.kanjj.mywebrtc.R;
import com.kanjj.mywebrtc.adapter.ChatItemAdapter;
import com.kanjj.mywebrtc.network.SocketClient;

import org.apache.commons.lang3.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 聊天的fragment
 */
public class ChatFragment extends Fragment implements View.OnClickListener {

    @BindView(R.id.chat_list_view)
    ListView chatListView;

    @BindView(R.id.chat_input_text)
    EditText chatInputText;

    @BindView(R.id.chat_send_btn)
    Button sendBtn;

    @BindView(R.id.chat_video_btn)
    Button video;

    private ChatItemAdapter chatItemAdapter;

    private ChatActivity chatActivity;

    private SocketClient socketClient;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_chat, container, false);
        ButterKnife.bind(this, view);
        video.setOnClickListener(this);
        sendBtn.setOnClickListener(this);
        // 初始化list数据
        chatItemAdapter = new ChatItemAdapter(getActivity());
        chatListView.setAdapter(chatItemAdapter);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        chatActivity = (ChatActivity) getActivity();
        socketClient = SocketClient.getInstance();
        initView();
    }

    public void initView() {
        socketClient.listener = new SocketClient.Listener() {
            @Override
            public void onMessage(String user, String msg) {
                System.out.println("==========================receive msg:" + msg);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        chatItemAdapter.addMsg(user, msg);
                        chatListView.setSelection(chatItemAdapter.getCount());
                    }
                });
            }
        };
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chat_send_btn:
                String s = chatInputText.getText().toString();
                if (StringUtils.isNoneBlank(s)) {
                    chatItemAdapter.addMsg(socketClient.userId, s);
                    socketClient.sendMsg(s);
                    chatInputText.setText("");
                }
                break;
            case R.id.chat_video_btn:
                chatActivity.gotoFragment(ChatActivity.VIDEO_FRAGMENT_TAG);
                break;
            default:
                break;
        }
    }
}